package eu.liquify.ui {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;
	public class Input extends UIItem {
		public var change:Function = NULL_FUNCTION;
		
		private var input:TextField = new TextField();
		public function Input(options:Object = null) {
			super(options);
			
			input.defaultTextFormat = new TextFormat("Tahoma", 12);
			//input.x = 10;
			//input.y = 3;
			input.width = 300;
			input.height = 100;
			input.type = TextFieldType.INPUT;
			input.border = true;
			input.borderColor = 0xB1B1B1;
			input.background = true;
			input.multiline = true;
			input.addEventListener(Event.CHANGE, dispatchChange);
			input.addEventListener(FocusEvent.FOCUS_IN, dispatchChangeDelayed);
			input.addEventListener(MouseEvent.MOUSE_DOWN, dispatchChangeDelayed);
			input.addEventListener(KeyboardEvent.KEY_DOWN, dispatchChangeDelayed);
			addChild(input);
			
			wordWrap = true;
			
			width = input.width;
			height = input.height;
		}
		
		/*
		override public function get label():String { return super.label; }
		
		override public function set label(value:String):void {
			super.label = value;
			titleTxt.text = label;
		}
		*/
		
		public function get wordWrap():Boolean { return input.wordWrap; }
		public function set wordWrap(v:Boolean):void { input.wordWrap = v; }
		
		override public function resize():void {
			super.resize();
			
			input.width = width;
			input.height = height;
		}
		
		public function get text():String {	return input.text; }
		public function set text(v:String):void { input.text = v; }
		
		public function selectAll():void {
			input.setSelection(0, input.length);
		}
		
		public function focus():void {
			stage.focus = input;
		}
		
		public function getLines():Vector.<String> {
			if (input.length == 0) return new Vector.<String>();
			var lines:Vector.<String> = new Vector.<String>(input.numLines);
			for (var i:int = 0; i < input.numLines; i++) {
				lines[i] = input.getLineText(i);
			}
			return lines;
		}
		
		public function getFocusedLine():String {
			var index:int = input.caretIndex;
			var lineIndex:int = index == input.length ? input.numLines-1 : input.getLineIndexOfChar(index);
			
			var line:String = lineIndex == -1 ? " " : input.getLineText(lineIndex);
			
			return line;
		}
		
		private function dispatchChangeDelayed(e:Event):void {
			addEventListener(Event.ENTER_FRAME, dispatchChange, false, 0, true);
		}
		private function dispatchChange(e:Event = null):void {
			removeEventListener(Event.ENTER_FRAME, dispatchChange);
			
			if (change != NULL_FUNCTION) Utils.callDynamic(change, this);
			
			dispatchEvent(new Event(Event.CHANGE));
		}
		
	}

}